package com.htht.smartschool.action.user;

import java.util.List;
import java.util.Map;

import com.htht.smartschool.action.baseaction.BaseAction;
import com.htht.smartschool.common.model.ParamData;
import com.htht.smartschool.common.servlet.DataContainer;
import com.htht.smartschool.model.roles.Role;
import com.htht.smartschool.model.student.TStudent;
import com.htht.smartschool.model.user.TUser;
import com.htht.smartschool.service.user.UserService;
import com.htht.smartschool.util.http.HttpUtil;
import com.htht.smartschool.util.system.GsonUtils;

public class UserAction extends BaseAction{

	@SuppressWarnings("rawtypes")
	public boolean processData(DataContainer obj,ParamData pd) {
		UserService userService = new UserService();
		boolean flag = false;
		TUser tuser = (TUser) GsonUtils.getInstance().fromJson(pd.getData(), TUser.class);
		Role  role = (Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
		if(pd.getCmd().equalsIgnoreCase("register")){
			TUser user = userService.register(tuser);
			if(user != null){
				flag = true;
				obj.setData(user);
			}else{
				obj.setData("注册失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("webRegister")){
			TUser user = userService.webRegister(tuser,role);
			if(user != null){
				flag = true;
				obj.setData(user);
			}else{
				obj.setData("注册失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("login")){
			TUser user = userService.login(tuser);
			if (user != null) {
				flag = true;
				obj.setData(user);
			} else {
				obj.setData("用户名或密码错误！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("loginout")){
			boolean result = userService.loginout(tuser.getPushsessionid());
			if (result) {
				obj.setData(result);
				flag = true;
			} else {
				obj.setData("退出失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("verifyByUsercode")){
			boolean verify = userService.verifyByUsercode(tuser.getUsercode(),tuser.getUsertype());
			flag = true;
			obj.setData(verify);
		}else if(pd.getCmd().equalsIgnoreCase("update")){
			TUser user = (TUser) userService.updateInfo(tuser);
			if (user != null) {
				flag = true;
				obj.setData(user);
			} else {
				obj.setData("修改用户信息失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getStudentByUserCode")){
			List<TStudent> list = userService.getStudentByUserCode(tuser.getUsercode());
			if (list.size() > 0) {
				flag = true;
				obj.setData(list);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getUsersByPageAndAttr")){
			Map map = userService.getUsersByPageAndAttr(tuser);
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("未查询到数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("lockUserByUsercodes")){
			boolean result = userService.lockUserByUsercodes(tuser);
			if(result){
				flag = true;
				obj.setData("锁定用户成功！");
			}else{
				obj.setData("锁定用户失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("unlockUserByUsercodes")){
			boolean result = userService.unlockUserByUsercodes(tuser);
			if(result){
				flag = true;
				obj.setData("解锁用户成功！");
			}else{
				obj.setData("解锁用户失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAllUserWithStudent")){
			Map map = userService.getAllUserWithStudent(tuser);
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查询失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAllAdmin")){
			Map map = userService.getAllAdmin(tuser);//获取所有管理员
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查询失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateUserInfo")){
			TUser user = (TUser) userService.updateUserInfo(tuser);
			if (user != null) {
				flag = true;
				obj.setData(user);
			} else {
				obj.setData("修改用户信息失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("delete")){
			boolean result = userService.deleteUser(tuser);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除用户失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getUserInfo")){
			TUser user = userService.getUserDetail(tuser.getUsercode());
			if(user!=null){
				flag = true;
				obj.setData(user);
			}else{
				obj.setData("获取信息失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("phoneVerification")){
			Boolean result = userService.getCode(tuser);//获取手机验证码
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("请检查用户名是否正确");
			}
		}else if(pd.getCmd().equalsIgnoreCase("codeVerification")){
			Boolean result = userService.codeVerification(tuser);//手机验证码验证
			if(result){
				flag =true;
				obj.setData(result);
			}else{
				obj.setData("验证失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("resetPassword")){
			boolean result = userService.resetPassword(tuser);//重置密码
			if(result){
				flag =true;
				obj.setData(result);
			}else {
				obj.setData("重置密码失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("mainTest")){
			List result = userService.mainTest();//重置密码
			if(result.size() > 0){
				flag =true;
				obj.setData(result);
			}else {
				obj.setData("重置密码失败");
			}
		}
			return flag;
		}
}
